<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>新特性</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="migration70.incompatible.html">« 不向后兼容的变更</a></li>
      <li style="float: right;"><a href="migration70.deprecated.html">PHP 7.0.x 弃用的功能 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="migration70.html">从 PHP 5.6.x 移植到 PHP 7.0.x</a></li>
    <li>新特性</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="migration70.new-features" class="sect1">
 <h2 class="title">新特性</h2>

 <div class="sect2" id="migration70.new-features.scalar-type-declarations">
  <h3 class="title">标量类型声明</h3>

  <p class="para">
   标量<a href="language.types.declarations.html" class="link">类型声明</a>
   有两种模式: 强制 (默认) 和 严格模式。 现在可以使用下列类型参数（无论用强制模式还是严格模式）：
   字符串(<span class="type">string</span>), 整数 (<code class="literal">int</code>), 浮点数 (<span class="type">float</span>), 
   以及布尔值 (<code class="literal">bool</code>)。它们扩充了PHP5中引入的其他类型：类名，接口，<span class="type"><span class="type 数组">数组</span></span>和
   <span class="type"><span class="type 回调类型">回调类型</span></span>。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Coercive&nbsp;mode<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">sumOfInts</span><span style="color: #007700">(</span><span style="color: #0000BB">int&nbsp;</span><span style="color: #007700">...</span><span style="color: #0000BB">$ints</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">array_sum</span><span style="color: #007700">(</span><span style="color: #0000BB">$ints</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sumOfInts</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'3'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4.1</span><span style="color: #007700">));</span>
</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(9)
</pre></div>
   </div>
  </div>

  <p class="para">
   要使用严格模式，一个 <a href="control-structures.declare.html" class="link"><code class="literal">declare</code></a> 声明指令必须放在文件的顶部。这意味着严格声明标量是基于文件可配的。
   这个指令不仅影响参数的类型声明，也影响到函数的返回值声明（参见
   <a href="language.types.declarations.html" class="link">返回值类型声明</a>,
   内置的PHP函数以及扩展中加载的PHP函数）
  </p>

  <p class="para">
   完整的标量类型声明文档和示例参见<a href="language.types.declarations.html" class="link">类型声明</a>章节。
   
  </p>
 </div>

 <div class="sect2" id="migration70.new-features.return-type-declarations">
  <h3 class="title">返回值类型声明</h3>

  <p class="para">
   PHP 7 增加了对<a href="language.types.declarations.html" class="link">返回类型声明</a>的支持。
   类似于<a href="language.types.declarations.html" class="link">参数类型声明</a>，返回类型声明指明了函数返回值的类型。可用的<a href="language.types.declarations.html" class="link">类型</a>与参数声明中可用的类型相同。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">arraysSum</span><span style="color: #007700">(array&nbsp;...</span><span style="color: #0000BB">$arrays</span><span style="color: #007700">):&nbsp;array<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(function(array&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">int&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">array_sum</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;},&nbsp;</span><span style="color: #0000BB">$arrays</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">arraysSum</span><span style="color: #007700">([</span><span style="color: #0000BB">1</span><span style="color: #007700">,</span><span style="color: #0000BB">2</span><span style="color: #007700">,</span><span style="color: #0000BB">3</span><span style="color: #007700">],&nbsp;[</span><span style="color: #0000BB">4</span><span style="color: #007700">,</span><span style="color: #0000BB">5</span><span style="color: #007700">,</span><span style="color: #0000BB">6</span><span style="color: #007700">],&nbsp;[</span><span style="color: #0000BB">7</span><span style="color: #007700">,</span><span style="color: #0000BB">8</span><span style="color: #007700">,</span><span style="color: #0000BB">9</span><span style="color: #007700">]));</span>
</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; 6
    [1] =&gt; 15
    [2] =&gt; 24
)
</pre></div>
   </div>
  </div>

  <p class="para">
   完整的标量类型声明文档和示例可参见 
   <a href="language.types.declarations.html" class="link">返回值类型声明</a>.
  </p>
 </div>

 <div class="sect2" id="migration70.new-features.null-coalesce-op">
  <h3 class="title">null合并运算符</h3>

  <p class="para">
   由于日常使用中存在大量同时使用三元表达式和 <span class="function"><a href="function.isset.html" class="function">isset()</a></span>的情况，
   我们添加了null合并运算符 (<code class="literal">??</code>) 这个语法糖。如果变量存在且值不为<strong><code>null</code></strong>，
   它就会返回自身的值，否则返回它的第二个操作数。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Fetches&nbsp;the&nbsp;value&nbsp;of&nbsp;$_GET['user']&nbsp;and&nbsp;returns&nbsp;'nobody'<br />//&nbsp;if&nbsp;it&nbsp;does&nbsp;not&nbsp;exist.<br /></span><span style="color: #0000BB">$username&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'user'</span><span style="color: #007700">]&nbsp;??&nbsp;</span><span style="color: #DD0000">'nobody'</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;This&nbsp;is&nbsp;equivalent&nbsp;to:<br /></span><span style="color: #0000BB">$username&nbsp;</span><span style="color: #007700">=&nbsp;isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'user'</span><span style="color: #007700">])&nbsp;?&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'user'</span><span style="color: #007700">]&nbsp;:&nbsp;</span><span style="color: #DD0000">'nobody'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Coalesces&nbsp;can&nbsp;be&nbsp;chained:&nbsp;this&nbsp;will&nbsp;return&nbsp;the&nbsp;first<br />//&nbsp;defined&nbsp;value&nbsp;out&nbsp;of&nbsp;$_GET['user'],&nbsp;$_POST['user'],&nbsp;and<br />//&nbsp;'nobody'.<br /></span><span style="color: #0000BB">$username&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'user'</span><span style="color: #007700">]&nbsp;??&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'user'</span><span style="color: #007700">]&nbsp;??&nbsp;</span><span style="color: #DD0000">'nobody'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  
 </div>

 <div class="sect2" id="migration70.new-features.spaceship-op">
  <h3 class="title">太空船操作符（组合比较符）</h3>
  <p class="para">
   太空船操作符用于比较两个表达式。当<var class="varname">$a</var>小于、等于或大于<var class="varname">$b</var>时它分别返回-1、0或1。
   比较的原则是沿用 PHP 的<a href="types.comparisons.html" class="link">常规比较规则</a>进行的。
  </p>
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;整数<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;-1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br /><br />//&nbsp;浮点数<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">1.5&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">1.5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">1.5&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">2.5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;-1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">2.5&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">1.5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br />&nbsp;<br />//&nbsp;字符串<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;-1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;1<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  
 </div>

 <div class="sect2" id="migration70.new-features.define-array">
  <h3 class="title">通过 <span class="function"><a href="function.define.html" class="function">define()</a></span> 定义常量数组 </h3>

  <p class="para">
   <span class="type">Array</span> 类型的常量现在可以通过
   <span class="function"><a href="function.define.html" class="function">define()</a></span> 来定义。在 PHP5.6 中仅能通过 <a href="language.constants.syntax.html" class="link"><code class="literal">const</code></a> 定义。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />define</span><span style="color: #007700">(</span><span style="color: #DD0000">'ANIMALS'</span><span style="color: #007700">,&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'dog'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'cat'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'bird'<br /></span><span style="color: #007700">]);<br /><br />echo&nbsp;</span><span style="color: #0000BB">ANIMALS</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;"cat"<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>

 <div class="sect2" id="migration70.new-features.anonymous-classes">
  <h3 class="title">匿名类</h3>

  <p class="para">
   现在支持通过<code class="literal">new class</code> 来实例化一个匿名类，这可以用来替代一些“用后即焚”的完整类定义。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">interface&nbsp;</span><span style="color: #0000BB">Logger&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">log</span><span style="color: #007700">(</span><span style="color: #0000BB">string&nbsp;$msg</span><span style="color: #007700">);<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">Application&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$logger</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">getLogger</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">Logger&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">logger</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">setLogger</span><span style="color: #007700">(</span><span style="color: #0000BB">Logger&nbsp;$logger</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">logger&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$logger</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$app&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Application</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$app</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLogger</span><span style="color: #007700">(new&nbsp;class&nbsp;implements&nbsp;</span><span style="color: #0000BB">Logger&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">log</span><span style="color: #007700">(</span><span style="color: #0000BB">string&nbsp;$msg</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$msg</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />});<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$app</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getLogger</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
<div class="cdata"><pre>
object(class@anonymous)#2 (0) {
}
</pre></div>
   </div>
  </div>

  <p class="para">
   详细文档可以参考
   <a href="language.oop5.anonymous.html" class="link">匿名类</a>.
  </p>
 </div>

 <div class="sect2" id="migration70.new-features.unicode-codepoint-escape-syntax">
  <h3 class="title">Unicode codepoint 转译语法</h3>

  <p class="para">
   这接受一个以16进制形式的 Unicode codepoint，并打印出一个双引号或heredoc包围的 UTF-8 编码格式的字符串。
   可以接受任何有效的 codepoint，并且开头的 0 是可以省略的。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
echo&nbsp;"\u{aa}";<br />echo&nbsp;"\u{0000aa}";<br />echo&nbsp;"\u{9999}";</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
<div class="cdata"><pre>
ª
ª (same as before but with optional leading 0&#039;s)
香
</pre></div>
   </div>
  </div>
 </div>

 <div class="sect2" id="migration70.new-features.closure-call-method">
  <h3 class="title"><span class="methodname"><a href="closure.call.html" class="methodname">Closure::call()</a></span></h3>

  <p class="para">
   <span class="methodname"><a href="closure.call.html" class="methodname">Closure::call()</a></span> 现在有着更好的性能，简短干练的暂时绑定一个方法到对象上闭包并调用它。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{private&nbsp;</span><span style="color: #0000BB">$x&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;}<br /><br /></span><span style="color: #FF8000">//&nbsp;PHP&nbsp;7&nbsp;之前版本的代码<br /></span><span style="color: #0000BB">$getXCB&nbsp;</span><span style="color: #007700">=&nbsp;function()&nbsp;{return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">x</span><span style="color: #007700">;};<br /></span><span style="color: #0000BB">$getX&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$getXCB</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bindTo</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'A'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;中间层闭包<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$getX</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;PHP&nbsp;7+&nbsp;及更高版本的代码<br /></span><span style="color: #0000BB">$getX&nbsp;</span><span style="color: #007700">=&nbsp;function()&nbsp;{return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">x</span><span style="color: #007700">;};<br />echo&nbsp;</span><span style="color: #0000BB">$getX</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">call</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">);</span>
</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
<div class="cdata"><pre>
1
1
</pre></div>
   </div>
  </div>
 </div>
 <div class="sect2" id="migration70.new-features.filtered-unserialize">
  <h3 class="title">为 <span class="function"><a href="function.unserialize.html" class="function">unserialize()</a></span> 提供过滤</h3>

  <p class="para">
   这个特性旨在提供更安全的方式解包不可靠的数据。它通过白名单的方式来防止潜在的代码注入。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;将所有的对象都转换为&nbsp;__PHP_Incomplete_Class&nbsp;对象<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">,&nbsp;[</span><span style="color: #DD0000">"allowed_classes"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;将除&nbsp;MyClass&nbsp;和&nbsp;MyClass2&nbsp;之外的所有对象都转换为&nbsp;__PHP_Incomplete_Class&nbsp;对象<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">,&nbsp;[</span><span style="color: #DD0000">"allowed_classes"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[</span><span style="color: #DD0000">"MyClass"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"MyClass2"</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;默认情况下所有的类都是可接受的，等同于省略第二个参数<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">,&nbsp;[</span><span style="color: #DD0000">"allowed_classes"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">]);</span>
</span>
</code></div>
   </div>

  </div>
 </div>

 <div class="sect2" id="migration70.new-features.intlchar">
  <h3 class="title"><span class="classname"><a href="class.intlchar.html" class="classname">IntlChar</a></span></h3>

  <p class="para">
   新增加的 <span class="classname"><a href="class.intlchar.html" class="classname">IntlChar</a></span> 类旨在暴露出更多的 ICU 功能。这个类自身定义了许多静态方法用于操作多字符集的 unicode 字符。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />printf</span><span style="color: #007700">(</span><span style="color: #DD0000">'%x'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">IntlChar</span><span style="color: #007700">::</span><span style="color: #0000BB">CODEPOINT_MAX</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">IntlChar</span><span style="color: #007700">::</span><span style="color: #0000BB">charName</span><span style="color: #007700">(</span><span style="color: #DD0000">'@'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">IntlChar</span><span style="color: #007700">::</span><span style="color: #0000BB">ispunct</span><span style="color: #007700">(</span><span style="color: #DD0000">'!'</span><span style="color: #007700">));</span>
</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
<div class="cdata"><pre>
10ffff
COMMERCIAL AT
bool(true)
</pre></div>
   </div>
  </div>

  <p class="para">
   若要使用此类，请先安装<a href="book.intl.html" class="link">Intl</a>扩展
  </p>
 </div>

 <div class="sect2" id="migration70.new-features.expectations">
  <h3 class="title">预期</h3>

  <p class="para">
   <a href="function.assert.html#function.assert.expectations" class="link">预期</a>是向后兼用并增强之前的 <span class="function"><a href="function.assert.html" class="function">assert()</a></span> 的方法。
   它使得在生产环境中启用断言为零成本，并且提供当断言失败时抛出特定异常的能力。
  </p>

   <p class="para">
    老版本的API出于兼容目的将继续被维护，<span class="function"><a href="function.assert.html" class="function">assert()</a></span>现在是一个语言结构，它允许第一个参数是一个表达式，而不仅仅是一个待计算的 <span class="type">string</span>或一个待测试的<span class="type">boolean</span>。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'assert.exception'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br />class&nbsp;</span><span style="color: #0000BB">CustomError&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">AssertionError&nbsp;</span><span style="color: #007700">{}<br /><br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">CustomError</span><span style="color: #007700">(</span><span style="color: #DD0000">'Some&nbsp;error&nbsp;message'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
<div class="cdata"><pre>
Fatal error: Uncaught CustomError: Some error message
</pre></div>
   </div>
  </div>

  <p class="para">
   关于这个特性的完整说明，包括如何在开发和生产环境中配置它，可以在<span class="function"><a href="function.assert.html" class="function">assert()</a></span>的
   <a href="function.assert.html#function.assert.expectations" class="link">expectations section</a>章节找到。
  </p>
 </div>
 <div class="sect2" id="migration70.new-features.group-use-declarations">
  <h3 class="title"><code class="literal">use</code> 组声明</h3>

  <p class="para">
  从同一 <a href="language.namespaces.definition.html" class="link"><code class="literal">namespace</code></a> 导入的类、函数和常量现在可以通过单个 <a href="language.namespaces.importing.html" class="link"><code class="literal">use</code></a> 语句
  一次性导入了。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;PHP&nbsp;7&nbsp;之前的代码<br /></span><span style="color: #007700">use&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\</span><span style="color: #0000BB">ClassA</span><span style="color: #007700">;<br />use&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\</span><span style="color: #0000BB">ClassB</span><span style="color: #007700">;<br />use&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\</span><span style="color: #0000BB">ClassC&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">;<br /><br />use&nbsp;function&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\</span><span style="color: #0000BB">fn_a</span><span style="color: #007700">;<br />use&nbsp;function&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\</span><span style="color: #0000BB">fn_b</span><span style="color: #007700">;<br />use&nbsp;function&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\</span><span style="color: #0000BB">fn_c</span><span style="color: #007700">;<br /><br />use&nbsp;const&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\</span><span style="color: #0000BB">ConstA</span><span style="color: #007700">;<br />use&nbsp;const&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\</span><span style="color: #0000BB">ConstB</span><span style="color: #007700">;<br />use&nbsp;const&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\</span><span style="color: #0000BB">ConstC</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;PHP&nbsp;7+&nbsp;及更高版本的代码<br /></span><span style="color: #007700">use&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\{</span><span style="color: #0000BB">ClassA</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ClassB</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ClassC&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">};<br />use&nbsp;function&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\{</span><span style="color: #0000BB">fn_a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">fn_b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">fn_c</span><span style="color: #007700">};<br />use&nbsp;const&nbsp;</span><span style="color: #0000BB">some</span><span style="color: #007700">\namespace\{</span><span style="color: #0000BB">ConstA</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ConstB</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ConstC</span><span style="color: #007700">};<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>
 <div class="sect2" id="migration70.new-features.generator-return-expressions">
  <h3 class="title">生成器可以返回表达式</h3>

  <p class="para">
   此特性基于 PHP 5.5 版本中引入的生成器特性构建的。
   它允许在生成器函数中通过使用 <code class="literal">return</code> 语法来返回一个表达式
   （但是不允许返回引用值），
   可以通过调用 <code class="literal">Generator::getReturn()</code> 方法来获取生成器的返回值，
   但是这个方法只能在生成器完成产生工作以后调用一次。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$gen&nbsp;</span><span style="color: #007700">=&nbsp;(function()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br />})();<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$gen&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br />echo&nbsp;</span><span style="color: #0000BB">$gen</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getReturn</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;</span>
</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
<div class="cdata"><pre>
1
2
3
</pre></div>
   </div>
  </div>

  <p class="para">
   在生成器中能够返回最终的值是一个非常便利的特性，
   因为它使得调用生成器的客户端代码可以直接得到生成器（或者其他协同计算）的返回值，
   相对于之前版本中客户端代码必须先检查生成器是否产生了最终的值然后再进行响应处理
   来得方便多了。
  </p>
 </div>
 <div class="sect2" id="migration70.new-features.generator-delegation">
  <h3 class="title">生成器委托</h3>

  <p class="para">
   现在，只需在最外层生成其中使用 <a href="language.generators.syntax.html#control-structures.yield.from" class="link"><code class="literal">yield from</code></a>，就可以把一个生成器自动委派给其他的生成器，<span class="classname"><a href="class.traversable.html" class="classname">Traversable</a></span>
   对象或者 <span class="type">array</span>。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">gen</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;from&nbsp;</span><span style="color: #0000BB">gen2</span><span style="color: #007700">();<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">gen2</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br />}<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">gen</span><span style="color: #007700">()&nbsp;as&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
<div class="cdata"><pre>
1
2
3
4
</pre></div>
   </div>
  </div>
 </div>
 <div class="sect2" id="migration70.new-features.intdiv">
  <h3 class="title">整数除法函数 <span class="function"><a href="function.intdiv.html" class="function">intdiv()</a></span></h3>

  <p class="para">
   新加的函数 <span class="function"><a href="function.intdiv.html" class="function">intdiv()</a></span> 用来进行
   整数的除法运算。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">intdiv</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(3)
</pre></div>
   </div>
  </div>
 </div>
  <div class="sect2" id="migration70.new-features.session-options">
  <h3 class="title">会话选项</h3>

  <p class="para">
   <span class="function"><a href="function.session-start.html" class="function">session_start()</a></span> 可以接受一个 <span class="type">array</span> 作为参数，
   用来覆盖 php.ini 文件中设置的
   <a href="session.configuration.html" class="link">会话配置选项</a>。
  </p>

  <p class="para">
   在调用 <span class="function"><a href="function.session-start.html" class="function">session_start()</a></span> 的时候，
   传入的选项参数中也支持 
   <a href="session.configuration.html#ini.session.lazy-write" class="link">session.lazy_write</a> 行为，
   默认情况下这个配置项是打开的。它的作用是控制 PHP 只有在会话中的数据发生变化的时候才
   写入会话存储文件，如果会话中的数据没有发生改变，那么 PHP 会在读取完会话数据之后，
   立即关闭会话存储文件，不做任何修改，可以通过设置 
   <code class="literal">read_and_close</code> 来实现。
  </p>

  <p class="para">
   例如，下列代码设置 
   <a href="session.configuration.html#ini.session.cache-limiter" class="link">session.cache_limiter</a> 为 
   <code class="literal">private</code>，并且在读取完毕会话数据之后马上关闭会话存储文件。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />session_start</span><span style="color: #007700">([<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'cache_limiter'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'private'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'read_and_close'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,<br />]);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>

 <div class="sect2" id="migration70.new-features.preg-repace-callback-array-function">
  <h3 class="title"><span class="function"><a href="function.preg-replace-callback-array.html" class="function">preg_replace_callback_array()</a></span></h3>

  <p class="para">
   在 PHP 7 之前，当使用 <span class="function"><a href="function.preg-replace-callback.html" class="function">preg_replace_callback()</a></span> 函数的时候，
   由于针对每个正则表达式都要执行回调函数，可能导致过多的分支代码。
   而使用新加的 <span class="function"><a href="function.preg-replace-callback-array.html" class="function">preg_replace_callback_array()</a></span> 函数，
   可以使得代码更加简洁。
  </p>

  <p class="para">
   现在，可以使用一个关联数组来对每个正则表达式注册回调函数，
   正则表达式本身作为关联数组的键，
   而对应的回调函数就是关联数组的值。
  </p>
 </div>

 <div class="sect2" id="migration70.new-features.csprng-functions">
  <h3 class="title">CSPRNG 函数</h3>

  <p class="para">
   新加入两个跨平台的函数：<span class="function"><a href="function.random-bytes.html" class="function">random_bytes()</a></span> 和 
   <span class="function"><a href="function.random-int.html" class="function">random_int()</a></span> 用来产生高安全级别的随机字符串和随机整数。
  </p>
 </div>

 <div class="sect2" id="migration70.new-features.list-arrayaccess">
  <h3 class="title">
   可以使用 <span class="function"><a href="function.list.html" class="function">list()</a></span> 函数来展开实现了
   <span class="classname"><a href="class.arrayaccess.html" class="classname">ArrayAccess</a></span> 接口的对象
  </h3>

  <p class="para">
   在之前版本中，<span class="function"><a href="function.list.html" class="function">list()</a></span> 函数不能保证
   正确的展开实现了 <span class="classname"><a href="class.arrayaccess.html" class="classname">ArrayAccess</a></span> 接口的对象，
   现在这个问题已经被修复。
  </p>
 </div>
 
 <div class="sect2" id="migration70.new-features.others">
  <h3 class="title">其他特性</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     允许在克隆表达式上访问对象成员，例如：
     <code class="literal">(clone $foo)-&gt;bar()</code>。
    </span>
   </li>
  </ul>
 </div>
</div></div></div></body></html>